// 
// DotNetNuke?- http://www.dotnetnuke.com 
// Copyright (c) 2002-2012 
// by DotNetNuke Corporation 
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions: 
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software. 
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE. 
// 

using System;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Web;
using System.Collections.Generic;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Search;
using DotNetNuke.Entities.Modules;

namespace YourCompany.Modules.$safeprojectname$
{

    /// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// The Controller class for $safeprojectname$ 
    /// </summary> 
    /// <remarks> 
    /// </remarks> 
    /// <history> 
    /// </history> 
    /// ----------------------------------------------------------------------------- 
    public class $safeprojectname$Controller : I$safeprojectname$Controller, ISearchable, IPortable
    {
        #region "Private Members"
        private IDataProvider dataProvider;
        #endregion

        #region "Constructor"
        /// <summary>
        /// Initializes a new instance of the <see cref="$safeprojectname$Controller"/> class.
        /// </summary>
        public $safeprojectname$Controller()
        {
            dataProvider = DataProvider.Instance();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="$safeprojectname$Controller"/> class.
        /// </summary>
        /// <param name="dataProvider">The data provider.</param>
        public $safeprojectname$Controller(IDataProvider dataProvider)
        {
            this.dataProvider = dataProvider;
        }
        #endregion

        #region "Public Methods"

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// gets an object from the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="moduleId">The Id of the module</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public List<$safeprojectname$Info> Get$safeprojectname$s(int moduleId)
        {

            return CBO.FillCollection<$safeprojectname$Info>(dataProvider.Get$safeprojectname$s(moduleId));

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// gets an object from the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="moduleId">The Id of the module</param> 
        /// <param name="itemId">The Id of the item</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public $safeprojectname$Info Get$safeprojectname$(int moduleId, int itemId)
        {
            return ($safeprojectname$Info)CBO.FillObject(dataProvider.Get$safeprojectname$(moduleId, itemId), typeof($safeprojectname$Info));
        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// adds an object to the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="obj$safeprojectname$">The $safeprojectname$Info object</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public int Add$safeprojectname$($safeprojectname$Info obj$safeprojectname$)
        {

            if (!string.IsNullOrEmpty(obj$safeprojectname$.Content.Trim()))
            {
              return  dataProvider.Add$safeprojectname$(obj$safeprojectname$.ModuleId, obj$safeprojectname$.Content, obj$safeprojectname$.CreatedByUser);
            }
            return Null.NullInteger;
        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// saves an object to the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="obj$safeprojectname$">The $safeprojectname$Info object</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void Update$safeprojectname$($safeprojectname$Info obj$safeprojectname$)
        {

            if (!string.IsNullOrEmpty(obj$safeprojectname$.Content.Trim()))
            {
                dataProvider.Update$safeprojectname$(obj$safeprojectname$.ModuleId, obj$safeprojectname$.ItemId, obj$safeprojectname$.Content, obj$safeprojectname$.CreatedByUser);
            }

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// deletes an object from the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="moduleId">The Id of the module</param> 
        /// <param name="itemId">The Id of the item</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void Delete$safeprojectname$(int moduleId, int itemId)
        {

            dataProvider.Delete$safeprojectname$(moduleId, itemId);

        }

        #endregion

        #region "Optional Interfaces"

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// GetSearchItems implements the ISearchable Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="modInfo">The ModuleInfo for the module to be Indexed</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(ModuleInfo modInfo)
        {

            SearchItemInfoCollection SearchItemCollection = new SearchItemInfoCollection();

            List<$safeprojectname$Info> col$safeprojectname$s = Get$safeprojectname$s(modInfo.ModuleID);
            foreach ($safeprojectname$Info obj$safeprojectname$ in col$safeprojectname$s)
            {
                SearchItemInfo SearchItem = new SearchItemInfo(modInfo.ModuleTitle, obj$safeprojectname$.Content, obj$safeprojectname$.CreatedByUser, obj$safeprojectname$.CreatedDate, modInfo.ModuleID, obj$safeprojectname$.ItemId.ToString(), obj$safeprojectname$.Content, "ItemId=" + obj$safeprojectname$.ItemId.ToString());
                SearchItemCollection.Add(SearchItem);
            }

            return SearchItemCollection;

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// ExportModule implements the IPortable ExportModule Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="moduleID">The Id of the module to be exported</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public string ExportModule(int moduleID)
        {
            string strXML = string.Empty;
            List<$safeprojectname$Info> col$safeprojectname$s = Get$safeprojectname$s(moduleID);
            if (col$safeprojectname$s.Count != 0)
            {
                strXML += "<$safeprojectname$s>";
                foreach ($safeprojectname$Info obj$safeprojectname$ in col$safeprojectname$s)
                {
                    strXML += "<$safeprojectname$>";
                    strXML += "<content>" + XmlUtils.XMLEncode(obj$safeprojectname$.Content) + "</content>";
                    strXML += "</$safeprojectname$>";
                }
                strXML += "</$safeprojectname$s>";
            }
            return strXML;

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// ImportModule implements the IPortable ImportModule Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="moduleID">The Id of the module to be imported</param> 
        /// <param name="content">The content to be imported</param> 
        /// <param name="version">The version of the module to be imported</param> 
        /// <param name="userId">The Id of the user performing the import</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void ImportModule(int moduleID, string content, string version, int userId)
        {

            XmlNode xml$safeprojectname$s = Globals.GetContent(content, "$safeprojectname$s");
            foreach (XmlNode xml$safeprojectname$ in xml$safeprojectname$s.SelectNodes("$safeprojectname$"))
            {
                $safeprojectname$Info obj$safeprojectname$ = new $safeprojectname$Info();
                obj$safeprojectname$.ModuleId = moduleID;
                obj$safeprojectname$.Content = xml$safeprojectname$.SelectSingleNode("content").InnerText;
                obj$safeprojectname$.CreatedByUser = userId;
                Add$safeprojectname$(obj$safeprojectname$);
            }
        }

        #endregion

    }
}